import Vue from 'vue'
export default{
    //配置
state: {
    num:1
  },
  actions: {

  },
  getters:{
    getNum(state){//获取器getters里state很近
      return state.num;
    },
    getTest(state){
      return `[[${state.num}]]`;
    },
    getName(state){
      return state.name;
    }
  },
  //更改也和state很近，第一个参数就是state
  mutations: {
    addNum(state){
      state.num ++;
    },
    addByNum(state,num){
      state.num += num;
    },
    addProps(state,name){
      //state.name = name; 
      if(!state.name){
        //实例对象.$xxx
        //构造函数.xxx
        Vue.set(state,'name',name);
      }else{
        state.name = name;
      }
    }
    //nutations对state的操作只能是同步的，异步的mutation
    // addByNumAsync(state,num){
    //   //只能是同步，否则丢失记录
    //   setTimeout(function(){
    //     state.num += num;
    //   },0);
    // }
  },
  actions:{
    //$store.getters||commit||state
    addByNumAction({commit},num){
      setTimeout(() => {
        commit('addByNum',num);
      }, 0);
    },
    addProps({commit},name){
      commit('addProps',name);
    }
  }
}